//
//  MainViewController.swift
//  XSina
//
//  Created by steven on 2017/6/28.
//  Copyright © 2017年 steven. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()

        tabBar.tintColor = UIColor.orange
        //创建首页
       addChildViewController(childController: HomeTableViewController(), title: "首页", imageName: "tabbar_home")
       addChildViewController(childController: HomeTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(childController: AddBtnViewController(), title: "", imageName: "")
        addChildViewController(childController: HomeTableViewController(), title: "广场", imageName: "tabbar_discover")
        addChildViewController(childController: HomeTableViewController(), title: "我", imageName: "tabbar_profile")
    }

    override func viewWillAppear(_ animated: Bool) {
      super.viewWillAppear(animated)
        self.setupAddBtn()
    }
    
    private func setupAddBtn() {
        tabBar.addSubview(addBtn)
        let width = UIScreen.main.bounds.size.width / CGFloat(viewControllers!.count)
        let rect = CGRect(x: 0, y: 0, width: width, height: tabBar.bounds.height)
        // 如果要放在tabbar的第三个，需要设置便宜
        addBtn.frame = rect.offsetBy(dx: width * 2, dy: 0)
    }
    
  
    // 懒加载定义加好按钮
    private lazy var addBtn:UIButton = {
    let addBtn = UIButton()
    addBtn.setImage(UIImage(named:"tabbar_compose_icon_add"), for: UIControlState.normal)
    addBtn.setImage(UIImage(named:"tabbar_compose_icon_add_highlighted"), for: UIControlState.highlighted)
    addBtn.setBackgroundImage(UIImage(named:"tabbar_compose_button"), for: UIControlState.normal)
    addBtn.setBackgroundImage(UIImage(named:"tabbar_compose_button_highlighted"), for: UIControlState.highlighted)
    addBtn .addTarget(self, action: #selector(MainViewController.addBtnPressed), for: UIControlEvents.touchUpInside)
        return addBtn
    }()
    
    func addBtnPressed() {
        print("点击成功")
    }
    
    /**
     private 为私有方法，在其他的类中没法调用
    */
    private func addChildViewController(childController:UIViewController,title:String,imageName:String) {
        
        // 获取应用名称
        let bundleName = Bundle.main.infoDictionary!["CFBundleExecutable"] as! String
        
        print(bundleName)
        
        childController.tabBarItem.image = UIImage(named: imageName)
        childController.tabBarItem.selectedImage = UIImage(named: imageName+"_highlighted")
        childController.title = title
        let nav = UINavigationController()
        nav .addChildViewController(childController)
        addChildViewController(nav)
    }

   

}
